# TODO: Add comment
# 
# Author: rogb
###############################################################################

setClass("MarketDataEquity",
		contains=c(
				"SituationDate",
				"BasicInstrument",
				"Price"
				)
)

MarketDataEquity <- function(SituationDate,Currency,Name,Price){
	new("MarketDataEquity",SituationDate=SituationDate,Currency=Currency,Name=Name,Price=Price)
}

# object <- MarketDataEquity(Sys.Date(),c("CHF","EUR"),c("SMI Index","SX5E Index"),c(6200,2750))

setMethod("show","MarketDataEquity",function(object){
			cat("Equity Market Data as of: ",situationDate.character(object),"\n",sep="")
			print(data.frame(Name=name(object),Currency=currency(object),Preis=price(object)))
		})

#################################################################################

setClass("MarketDataVolatilitySurface",
		contains=c(
				"SituationDate",
				"BasicInstrument",
				"list"
				)
)

# vvv <- matrix(runif(10*6),nrow=10)
# vvv[sample(1:length(vvv),30,replace=F)] <- NA
# v1 <- VolatilitySurface(Sys.Date(),Name="SMI Index",Currency="CHF",Date=Sys.Date()+(1:10)*100,Strike=80+(0:5)*10,vvv)
# v2 <- VolatilitySurface(Sys.Date(),Name="SX5E Index",Currency="EUR",Date=Sys.Date()+(1:10)*100,Strike=80+(0:5)*10,vvv)
# dotdotdot <- list(v1,v2)

MarketDataVolatilitySurface <- function(...){
	dotdotdot <- list(...)
	Currency <- sapply(dotdotdot,currency)
	SituationDate <- sapply(dotdotdot,situationDate)
	if(all(sapply(SituationDate,function(x,y)x==y,SituationDate))){
		SituationDate <- as.Date(SituationDate[1],origin="1970-01-01")
	}
	else{
		stop("Situation Dates are inconsitent!")
	}
	Name <- sapply(dotdotdot,name)
	names(dotdotdot) <- make.names(Name)
	new("MarketDataVolatilitySurface",SituationDate=SituationDate,Currency=Currency,Name=Name,dotdotdot)
}

# object <- MarketDataVolatilitySurface(v1[,],v2[,])
setMethod("show","MarketDataVolatilitySurface",function(object){
			tmp <- lapply(object,function(x){show(x);cat("\n",sep="")})
		})

setMethod("plot","MarketDataVolatilitySurface",function(x,y,...){
			nrow <- floor(sqrt(length(x)))
			ncol <- ceiling(length(x)/nrow)
			par(mfrow=c(nrow,ncol))
			tmp <- lapply(x,plot)
		})

setMethod("[",signature=c("MarketDataVolatilitySurface","numeric","missing"),function(x,i,j,drop){			
			SituationDate <- situationDate(x)
			Name <- name(x)[i]
			Currency <- currency(x)[i]
			.Data <- x@.Data[i]
			new("MarketDataVolatilitySurface",SituationDate=SituationDate,Currency=Currency,Name=Name,.Data)
		})

setMethod("[",signature=c("MarketDataVolatilitySurface","character","missing"),function(x,i,j,drop){			
			i <- which(is.element(name(x),i))
			x[i]
		})

#################################################################################

setClass("MarketDataEQ",
		representation=list(
				EQ="MarketDataEquity",
				EQ.VOL="MarketDataVolatilitySurface"				
		)
)
# eq1 <- MarketDataEquity(Sys.Date(),c("CHF","EUR"),c("SMI Index","SX5E Index"),c(6200,2750))
# vvv <- matrix(runif(10*6),nrow=10)
# vvv[sample(1:length(vvv),30,replace=F)] <- NA
# v1 <- VolatilitySurface(Sys.Date(),Name="SMI Index",Currency="CHF",Date=Sys.Date()+(1:10)*100,Strike=80+(0:5)*10,vvv)
# v2 <- VolatilitySurface(Sys.Date(),Name="SX5E Index",Currency="EUR",Date=Sys.Date()+(1:10)*100,Strike=80+(0:5)*10,vvv)
# eq.v1 <- MarketDataVolatilitySurface(v1[,],v2[,])

MarketDataEQ <- function(MarketDataEquity,MarketDataVolatilitySurface){
	new("MarketDataEQ",EQ=MarketDataEquity,EQ.VOL=MarketDataVolatilitySurface)
}

# mdeq <- MarketDataEQ(eq1,eq.v1)




